//
//  ImageDetailViewController.m
//  WWXELuShang
//
//  Created by lanou on 15/12/19.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "ImageDetailViewController.h"
#import "ImageDetailModel.h"
#import "ImageDetailView.h"

@interface ImageDetailViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong)NSMutableArray *imageDetailArray;

@property (nonatomic, strong)ImageDetailView *imageDetailView;



@end

@implementation ImageDetailViewController




- (NSMutableArray *)imageDetailArray {
    if (!_imageDetailArray) {
        _imageDetailArray = [[NSMutableArray alloc]init];
    }
    return _imageDetailArray;
}
- (ImageDetailView *)imageDetailView {
    if (!_imageDetailView) {
        _imageDetailView = [[ImageDetailView alloc]initWithFrame:[UIScreen alloc].bounds];
        
    }
    
    return _imageDetailView;
}
- (void)jsonImageDetail {
    

    [LORequestManger GET:[NSString stringWithFormat:kImageDetailUrl, self.newsID] success:^(id response) {
        NSDictionary *rootDic = (NSDictionary *)response;
        NSDictionary *dataDic = rootDic[@"data"];
        self.imageDetailView.titleLabel.text = dataDic[@"title"];
        for (NSDictionary *dic in dataDic[@"albums"]) {
            ImageDetailModel *imageDetail = [[ImageDetailModel alloc]init];
            [imageDetail setValuesForKeysWithDictionary:dic];
            [self.imageDetailArray addObject:imageDetail];
        }
        [self.imageDetailView setValueWithScrollView:self.imageDetailArray target:self];
        ImageDetailModel *imageDetail = self.imageDetailArray[0];
        [self.imageDetailView setDetail:imageDetail.content];
        
        self.navigationItem.title = [NSString stringWithFormat:@"1/%ld",self.imageDetailArray.count];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
    
    
    
}




- (void)viewDidLoad {
    [super viewDidLoad];
    self.view = self.imageDetailView;
    self.navigationController.navigationBar.translucent = YES;

    
    [self jsonImageDetail];
    // Do any additional setup after loading the view.
}

//scrollView代理
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger page = scrollView.contentOffset.x / kWidth;
    ImageDetailModel *imageDetail = self.imageDetailArray[page];
    self.navigationItem.title = [NSString stringWithFormat:@"%ld/%ld", page + 1,self.imageDetailArray.count];
    
    [self.imageDetailView setDetail:imageDetail.content];
    
    
}




- (void)viewWillDisappear:(BOOL)animated {
    self.navigationController.navigationBar.translucent = NO;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
